package com.sleep.getexcel.service.impl;

import com.mysql.cj.util.StringUtils;
import com.sleep.getexcel.dao.KpiLabelMapper;
import com.sleep.getexcel.pojo.KpiLabel;
import com.sleep.getexcel.service.KpiLabelService;
import com.sleep.getexcel.utils.ReadExcel;
import com.sleep.getexcel.vo.FilePath;
import com.sleep.getexcel.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Sleep-6000
 * @date 2022/8/3
 */
@Service
@Transactional
public class KpiLabelServiceImpl implements KpiLabelService {
    @Autowired
    private KpiLabelMapper kpiLabelMapper;

    @Override
    public Result insertSJLabel(FilePath filePath) {
        String readPath = filePath.getReadPath();
        if (StringUtils.isNullOrEmpty(readPath)) {
            return Result.fail(10003, "参数错误");
        }
        ReadExcel readExcel = new ReadExcel();
        if (!readExcel.preReadCheck(readPath)) {
            return Result.fail(10001, "文件不存在");
        }
        List<List<String>> read = readExcel.read(readPath);
        if (read.size() == 0) {
            return Result.fail(10002, "文件为空");
        }
        int count = kpiLabelMapper.getCount();
        int success = 0;
        String dept = read.get(1).get(3);
        String duty = read.get(1).get(4);
        for (int i = 1; i < read.size(); i++) {
            KpiLabel label = new KpiLabel();
            label.setColId(Long.valueOf(read.get(i).get(0)));
            label.setLabelName(read.get(i).get(1));
            label.setLabelCode(read.get(i).get(2));
            label.setGetType("MAN");
            label.setGetPeriod("DAY");
            label.setTimeGrading("DAY");
            label.setDept(dept);
            label.setDutyPerson(duty);
            label.setDataSrc("LOCAL");
            label.setDataType(2);
            label.setNo(++count);
            label.setDataState(0);
            success += kpiLabelMapper.insert(label);
        }
        return Result.success(success);
    }


}
